package com.speedata;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.speedata.uhftest.R;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

/**
 * @author xuyan  展锐指示灯
 */
public class LightsActivity extends AppCompatActivity implements View.OnClickListener {

    private Button bt1;
    private Button bt2;
    private Button bt3;
    private Button bt4;
    private Button bt5;
    private Button bt6;
    private Button bt7;
    private Button bt8;
    private Button bt9;
    private Button bt10;
    private Button bt11;
    private Button bt12;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_light);
        initView();

//        byte[] a = {-58, -69, -71, -5, 65, 80, 80, 76, 69};
//        String b = "";
//       // try {
//            //b = new String(a, "gbk");
//            b = new String(a, StandardCharsets.UTF_8);
//       // } catch (UnsupportedEncodingException e) {
//        //    e.printStackTrace();
//       // }
//        Logcat.d(b);
    }

    private void initView() {
        bt1 = findViewById(R.id.bt1);
        bt2 = findViewById(R.id.bt2);
        bt3 = findViewById(R.id.bt3);
        bt4 = findViewById(R.id.bt4);
        bt5 = findViewById(R.id.bt5);
        bt6 = findViewById(R.id.bt6);
        bt7 = findViewById(R.id.bt7);
        bt8 = findViewById(R.id.bt8);
        bt9 = findViewById(R.id.bt9);
        bt10 = findViewById(R.id.bt10);
        bt11 = findViewById(R.id.bt11);
        bt12 = findViewById(R.id.bt12);

        bt1.setOnClickListener(this);
        bt2.setOnClickListener(this);
        bt3.setOnClickListener(this);
        bt4.setOnClickListener(this);
        bt5.setOnClickListener(this);
        bt6.setOnClickListener(this);
        bt7.setOnClickListener(this);
        bt8.setOnClickListener(this);
        bt9.setOnClickListener(this);
        bt10.setOnClickListener(this);
        bt11.setOnClickListener(this);
        bt12.setOnClickListener(this);


    }


    @SuppressLint({"HardwareIds", "NonConstantResourceId"})
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt1:
                try {
                    Power(Red, "1");
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.bt2:
                try {
                    Power(Red, "0");
                    Power(Green, "1");
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.bt3:
                try {
                    Power(Green, "0");
                    Power(Blue, "1");
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.bt4:
                try {
                    Power(Blue, "0");
                    Power(R1 + DIRECTION, OUT);
                    Power(R1 + VALUE, "1");
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.bt5:
                try {
                    Power(R1 + VALUE, "0");
                    Power(G1 + DIRECTION, OUT);
                    Power(G1 + VALUE, "1");
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.bt6:
                try {
                    Power(G1 + VALUE, "0");
                    Power(B1 + DIRECTION, OUT);
                    Power(B1 + VALUE, "1");
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.bt7:
                try {
                    Power(B1 + VALUE, "0");
                    Power(R2 + DIRECTION, OUT);
                    Power(R2 + VALUE, "1");
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.bt8:
                try {
                    Power(R2 + VALUE, "0");
                    Power(G2 + DIRECTION, OUT);
                    Power(G2 + VALUE, "1");
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.bt9:
                try {
                    Power(G2 + VALUE, "0");
                    Power(B2 + DIRECTION, OUT);
                    Power(B2 + VALUE, "1");
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.bt10:
                try {
                    Power(B2 + VALUE, "0");
                    Power(LED + DIRECTION, OUT);
                    Power(LED + VALUE, "1");
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.bt11:
                try {
                    Power(LED + VALUE, "0");
                    Power(BEIDOU + DIRECTION, OUT);
                    Power(BEIDOU + VALUE, "1");
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.bt12:
                try {
                    Power(BEIDOU + VALUE, "0");
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            default:
                break;
        }
    }


    private final String Red = "/sys/class/leds/sc27xx:red/brightness/";
    private final String Green = "/sys/class/leds/sc27xx:green/brightness/";
    private final String Blue = "/sys/class/leds/sc27xx:blue/brightness/";
    private final String R1 = "/sys/class/gpio/gpio99/";
    private final String G1 = "/sys/class/gpio/gpio204/";
    private final String B1 = "/sys/class/gpio/gpio95/";
    private final String R2 = "/sys/class/gpio/gpio98/";
    private final String G2 = "/sys/class/gpio/gpio140/";
    private final String B2 = "/sys/class/gpio/gpio205/";
    private final String LED = "/sys/class/gpio/gpio206/";
    private final String BEIDOU = "/sys/class/gpio/gpio123/";

    private final String VALUE = "value/";
    private final String DIRECTION = "direction/";
    private final String OUT = "out";


    public void Power(String path, String msg) throws IOException {
        try {
            Log.d("xuyanshuai", "PowerOnRed");
            BufferedWriter CtrlFile = new BufferedWriter(new FileWriter(path, false));
            CtrlFile.write(msg);
            CtrlFile.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void onResume() {
        super.onResume();


    }

    @Override
    protected void onPause() {
//        try {
//            Power(LED + VALUE, "0");
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
        super.onPause();
    }
}
